package com.group.eams.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.InventoryTaskQueryDto;
import com.group.eams.pojo.entity.InventoryTask;

import com.group.eams.pojo.entity.InventoryTask;
import com.group.eams.pojo.vo.InventoryTaskListVo;
import com.group.eams.service.InventoryTaskService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 盘点任务(InventoryTask)表控制层
 *
 * @author
 * @since 2024-10-30 12:58:28
 */
@RestController
@RequestMapping("/inventoryTask")
public class InventoryTaskController {
    /**
     * 服务对象
     */
    @Autowired
    private InventoryTaskService inventoryTaskService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "盘点任务表列表")
    public Result list(InventoryTaskQueryDto inventoryTaskQueryDto) {

        //1.准备分页条件
        Page<InventoryTask> page = new Page<>(inventoryTaskQueryDto.getPage(), inventoryTaskQueryDto.getLimit());

        //2.设置查询条件
        Page<InventoryTaskListVo> inventoryTaskByPage = inventoryTaskService.findInventoryTaskByPage(page, inventoryTaskQueryDto);

        return Result.buildSuccess(inventoryTaskByPage.getTotal(), inventoryTaskByPage.getRecords());
    }

    @RequestMapping(value = "/find", method = RequestMethod.GET)
    @ApiOperation(value = "根据id查找盘点任务表")
    public Result find(@RequestParam Long id) {
        InventoryTask inventoryTask = inventoryTaskService.getById(id);
        return Result.buildSuccess(inventoryTask);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加盘点任务表")
    public Result add(@RequestBody InventoryTask inventoryTask) {
        boolean result = inventoryTaskService.save(inventoryTask);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改盘点任务表")
    public Result update(@RequestBody InventoryTask inventoryTask) {
        boolean result = inventoryTaskService.updateById(inventoryTask);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    @ApiOperation("删除盘点任务表")
    public Result delete(@RequestParam Long id) {
        boolean result = inventoryTaskService.removeById(id);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchRemove", method = RequestMethod.POST)
    @ApiOperation("批量删除盘点任务表")
    public Result batchDelete(@RequestBody BatchDeleteDto batchDeleteDto) {
        boolean result = inventoryTaskService.removeByIds(batchDeleteDto.getIds());
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    @ApiOperation("查询所有盘点任务表")
    public Result findAll() {
        List<InventoryTask> inventoryTaskList = inventoryTaskService.list();
        return Result.buildSuccess(inventoryTaskList);
    }

    @RequestMapping(value = "/batchAdd", method = RequestMethod.POST)
    @ApiOperation("批量添加保修记录")
    public Result batchAdd(@RequestBody List<InventoryTask> inventoryTasks) {
        boolean result = inventoryTaskService.saveBatch(inventoryTasks);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    @RequestMapping(value = "/batchUpdate", method = RequestMethod.POST)
    @ApiOperation("批量修改保修记录")
    public Result batchUpdate(@RequestBody List<InventoryTask> inventoryTasks) {
        boolean result = inventoryTaskService.updateBatchById(inventoryTasks);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }
}

